﻿using System;
using System.Runtime.Serialization;
using System.Text.Json;

namespace Models.comm
{
    /// <summary>
    /// api返回值
    /// ErLin Li
    /// 2021/4/15
    /// </summary>
    [DataContract]
    public class ApiMessageEntity
    {
        /// <summary>
        /// 返回-1：身份验证失败，0：失败，1：成功， 2：异常（catch捕捉）
        /// </summary>
        [DataMember]
        public ApiReturnState requstResult
        { get; set; }

        /// <summary>
        /// 要返回的数据
        /// </summary>
        //[DataMember(Order = 2)]
        public object data
        { get; set; }

        /// <summary>
        /// 返回条数
        /// </summary>
        //[DataMember(Order = 3)]
        public int total
        { get; set; }

        [DataMember]
        public string msg { get; set; }

        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <param name="total">返回的数据条数</param>
        /// <returns></returns>
        public static ApiMessageEntity Ok(object data, int total = 0)
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Success, data = data, total = total };
        }
        public static ApiMessageEntity Ok()
            => new ApiMessageEntity { requstResult = ApiReturnState.Success };
        public static ApiMessageEntity Ok(string msg)
            => new ApiMessageEntity { requstResult = ApiReturnState.Success, msg = msg };
        public static ApiMessageEntity Ok(string msg, object obj)
            => new ApiMessageEntity { requstResult = ApiReturnState.Success, msg = msg, data = obj };
        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        public static ApiMessageEntity Ok(object data)
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Success, data = data };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity DefaultError()
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Fail, msg = "服务出错了" };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity Error(string msg)
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Fail, msg = msg };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity Error(Exception exception)
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Fail, msg = JsonSerializer.Serialize(exception) };
        }
        public static ApiMessageEntity Exception(string msg)
        {
            return new ApiMessageEntity { requstResult = ApiReturnState.Exception, msg = msg };
        }


    }

    /// <summary>
    /// 接口调用的返回状态
    /// </summary>
    public enum ApiReturnState
    {
        /// <summary>
        /// 身份验证失败
        /// </summary>
        AuthenticationFailure = -1,
        /// <summary>
        /// 失败
        /// </summary>
        Fail = 0,
        /// <summary>
        /// 成功
        /// </summary>
        Success = 1,
        /// <summary>
        /// 发生异常
        /// </summary>
        Exception = 2
    }
}
